/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.sessionbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.Category;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Breath
 */
@Stateless
public class CategoryFacade extends AbstractFacade<Category> implements CategoryFacadeLocal {
    @PersistenceContext(unitName = "com.blog.city_CheckTheDep_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    /**
     * 
     * @return
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    /**
     * 
     * @param em
     */
    protected void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * 
     */
    public CategoryFacade() {
        super(Category.class);
    }
    
    /**
     * 
     * @param name
     * @return
     */
    @Override
    public Category findCategoryByName(String name){
         return (Category) this.getEntityManager().createQuery("SELECT c FROM Category c WHERE c.name = :name").setParameter("name", name).getSingleResult();
    }
    
    
    
    /**
     * 
     * @param cat
     * @return
     */
    @Override
    public List<Article> findArticleByCategory(Category cat) {
        return (List<Article>) getEntityManager().createQuery("SELECT c FROM article_has_category c WHERE c.category_id= :category_id").setParameter("category_id", cat).getResultList();
    }
    
    
    /**
     * This method is not used because we had problems with deserializing the object. Could not get back the actual list. The problem was also 
     * Arquillian in tests.
     * @param cat
     * @return
     */
    @Override
    public List<Article> findArticleByCategoryNotUsed(Category cat) {
        return (List<Article>)cat.getArticleList();
    }
    
}
